﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.Commands;

namespace Mimoza.Apps.Console.ViewModel
{
    public class PolicyViewModel : BaseViewModel
    {
        private Container m_currentContainer = null;

        public Container CurrentContainer
        {
            get { return m_currentContainer; }
            set
            {
                if (m_currentContainer != value)
                {
                    m_currentContainer = value;
                    NotifyPropertyChanged("CurrentContainer");
                }
            }
        }
        public override void Init(Mimoza.Apps.Console.Application application)
        {
            base.Init(application);

            m_application.ContainersTreeViewModel.PropertyChanged +=new System.ComponentModel.PropertyChangedEventHandler(ContainersTreeViewModel_PropertyChanged);
            m_application.ObjectLayer.PolicyRepository.OnObjectChanged += new EventHandler<Mimoza.ObjectLayer.ObjectLayer_ObjectChangedEventArgs>(PolicyRepository_OnObjectChanged);

            m_ToolBarCommands.Add(new EditPolicyCommand(this));
            m_ToolBarCommands.Add(new DeletePolicyCommand(this)); 

            m_ContextCommands.Add(new EditPolicyCommand(this)); 
            m_ContextCommands.Add(new RenamePolicyCommand(this)); 
            m_ContextCommands.Add(new DeletePolicyCommand(this));             
        }

        void PolicyRepository_OnObjectChanged(object sender, Mimoza.ObjectLayer.ObjectLayer_ObjectChangedEventArgs e)
        {
            RaiseOnObjectChanged(e);
        }

        void ContainersTreeViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Context")
            {
                Container cont = (sender as BaseViewModel).Context as Container;

                if (null != cont && cont.Type == ContainerType.Managed )
                {
                    this.CurrentContainer = cont;
                    this.Visible = true;
                }
                else
                {
                    this.CurrentContainer = null;
                    this.Visible = false;
                }
            }
        }
    }
}
